package com.lihua.newideas.controller;

import com.lihua.newideas.pojo.MyUser;
import com.lihua.newideas.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    MyUserService userService;

    /*用户个人中心*/
    @RequestMapping("/userCenter")
    public  String userCenter(){

        return "grzx";
    }
    /*管理员个人中心*/
    @RequestMapping("/userCenter/{userName}")
    public  String adminUserCenter(Model model , @PathVariable String userName){
//        System.out.println("Ok");
//        System.out.println(userName);
        if (userName!=null){
            MyUser myUser = userService.queryUserByName(userName);
            if (myUser!=null){
                myUser.setPassWord("");
            }
            model.addAttribute("user",myUser);
        }else {
            model.addAttribute("user","用户没有登录过期");
        }
        return "grzx";
    }

    /*根据用户名修改用户信息
    * */
    @RequestMapping("/update")
    public String updateUser( MyUser user, Model model){
//        System.out.println("传进来的"+user.toString());
        String s = userService.updateUserByUserName(user);
        adminUserCenter(model,user.getUserName());
//        System.out.println(s);
        return "grzx";
    }


    /*
     * 查询所有用户
     * */
    @GetMapping("/admin/userList")
    public String  queryUserAll(Model model){
        List<MyUser> myUsers = userService.queryUserAll();
        model.addAttribute("userList",myUsers);
        return "admin/user_list";
    }

    /*根据用户id删除用户
     * */
    @RequestMapping("/admin/deleteUser/{id}")
    @ResponseBody
    String deleteMyUserById(@PathVariable("id") String id){
//        System.out.println(id);
        userService.deleteMyUserById(Integer.valueOf(id));

        return "删除成功";
    }
}
